﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.IO;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using System.Text.RegularExpressions;
using Models;
using BLL;

namespace UI
{
    public partial class zhuce : System.Web.UI.Page
    {

        

        Random Rdm = new Random();
        //产生0到100的随机数

        //接口
        private const String host = "http://dingxin.market.alicloudapi.com";
        private const String path = "/dx/sendSms";
        private const String method = "POST";
        private const String appcode = "dd39170f010a4b979a2649334c3f0d23";
        int suiji;
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        //发短信方法
        private bool get_YZM()
        {
            //接口代码开始
            int iRdm = Rdm.Next(100000, 999999);
            Session["suiji"] = iRdm;
            suiji = iRdm;
            Console.WriteLine(iRdm);
            string sjh = this.sj.Text.Trim();
            String querys = "mobile=" + sjh + "&param=code%3A" + iRdm + "&tpl_id=TP1711063";
            String bodys = "";
            String url = host + path;
            HttpWebRequest httpRequest = null;
            HttpWebResponse httpResponse = null;

            if (0 < querys.Length)
            {
                url = url + "?" + querys;

            }

            if (host.Contains("https://"))
            {

                ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
                httpRequest = (HttpWebRequest)WebRequest.CreateDefault(new Uri(url));

            }
            else
            {
                httpRequest = (HttpWebRequest)WebRequest.Create(url);
            }
            httpRequest.Method = method;
            httpRequest.Headers.Add("Authorization", "APPCODE " + appcode);
            if (0 < bodys.Length)
            {

                byte[] data = Encoding.UTF8.GetBytes(bodys);
                using (Stream stream = httpRequest.GetRequestStream())
                {
                    stream.Write(data, 0, data.Length);
                }

            }
            try
            {
                httpResponse = (HttpWebResponse)httpRequest.GetResponse();

            }

            catch (WebException ex)
            {
                httpResponse = (HttpWebResponse)ex.Response;
            }

            Console.WriteLine(httpResponse.StatusCode);
            Console.WriteLine(httpResponse.Method);
            Console.WriteLine(httpResponse.Headers);
            Stream st = httpResponse.GetResponseStream();
            StreamReader reader = new StreamReader(st, Encoding.GetEncoding("utf-8"));
            Console.WriteLine(reader.ReadToEnd());
            Console.WriteLine("\n");
            //接口 结束
            return true;
        }
        //获取验证码事件
        protected void Button1_Click(object sender, EventArgs e)
        {
            bool result = get_YZM();
            if (result)
            {
                Button1.Text = "短信已发送";
                Button1.Enabled = false;
                ////倒计时
                //for (int i = 60; i >= 0; i--)
                //{
                //    Button1.Text = i+"s";
                //    Thread.Sleep(1000);
                //}
                //Button1.Text = "发送验证码";
                //Button1.Enabled = true;
            }
        }
        public static bool CheckValidationResult(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors)
        {

            return true;
        }

        protected void TextBox3_TextChanged(object sender, EventArgs e)
        {

        }

        protected void TextBox2_TextChanged(object sender, EventArgs e)
        {

        }

        protected void TextBox2_DataBinding(object sender, EventArgs e)
        {

        }

        protected void TextBox2_Disposed(object sender, EventArgs e)
        {

        }

        //public bool IsHandset(string str_handset)
        //{

        //    return System.Text.RegularExpressions.Regex.IsMatch(str_handset, @"/^1[34578]\d{9}$");

        //}
        protected void Button2_Click(object sender, EventArgs e)
        {

            if (TextBox1.Text == "" || TextBox2.Text == "" || sj.Text == "" || TextBox3.Text == "" || TextBox4.Text == "")
            {
                Response.Write("<script>alert('请将信息填写完整！')</script>");
            }
            else
            {

                string sjsj = @"^1[34578]\d{9}$";
                Regex sjh = new Regex(sjsj);
                string phone = this.sj.Text.Trim();

                if (sjh.IsMatch(phone) == true)
                {
                    if (TextBox1.Text.Trim() == TextBox2.Text.Trim())//判断密码是否一致
                    {
                        int zhi = int.Parse(this.TextBox3.Text.Trim());
                        if (int.Parse(Session["suiji"].ToString()) == zhi)//判断用户输入的随机数是否一致
                        {
                            //全部一致则存入数据库并且打开主页面
                            Consumer1 cus = new Consumer1();
                            cus.Name = this.TextBox4.Text.Trim();
                            cus.Phone = this.sj.Text.Trim();
                            Session["name"] = this.TextBox4.Text.Trim();
                            cus.PassWord = this.TextBox2.Text.Trim();
                            //判断是否存储进数据库
                            if (BLL.AddBLL.Addkehu(cus))
                            {
                                Response.Write("<script>alert('注册成功！以自动为您登录，点击确定跳转到主页！');window.location.href='index.aspx';</script>");
                            }

                        }
                        else
                        {
                            Response.Write("<script>alert('验证码输入错误！')</script>");
                        }

                    }
                    else
                    {
                        Response.Write("<script>alert('前后密码输入不一致！')</script>");

                    }
                }
                else
                {
                    Response.Write("<script>alert('手机号输入不正确！')</script>");
                }



            }
        }
        //以下代码防止两个属性为password的文本框点击后文本内容消失
        protected override void OnPreRender(EventArgs args)
        { base.OnPreRender(args); TextBox1.Attributes["value"] = TextBox1.Text;
        base.OnPreRender(args); TextBox2.Attributes["value"] = TextBox2.Text;
        }
        //protected override void OnPreRender1(EventArgs args)
        //{ base.OnPreRender(args); TextBox2.Attributes["value1"] = TextBox2.Text; }
        //代码结束
    }
}